﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Generic.Services;
using ScrumTable.DL.Data.Schema.ScrumTableV1;

#endregion

namespace ScrumTable.DL.Data.Generic.Test.Mocks
{
    /// <summary>
    /// This is the test class for IDataLoader class.
    /// </summary>
    public class DataLoaderMock : IDataLoader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ResourceManager _resources;

        private CacheProject _project1;
        private CacheProject _project2;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        internal string FirstProjectUniqueId
        {
            get { return _project1.UniqueId; }
        }

        internal ResourceManager Resources
        {
            get { return _resources; }
        }

        internal ScrumTableV1Schema Schema { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataLoaderMock instance.
        /// </summary>
        internal DataLoaderMock()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the data which is required to connect to the back end tfs
        /// system.
        /// </summary>
        /// <param name="dataContext">Specifies the data context which contains the underlying data driver root object.</param>
        /// <param name="callback">Specifies the callback used to retrieve the connection state. The callback value is of type DataResult.</param>
        public void LoadConnectionData(GenericDataContextBase dataContext, AsyncCallback callback)
        {
            Assert.IsNotNull(callback);

            CacheContext context = new CacheContext(Schema);
            _resources = new ResourceManager(context);
            _project1 = _resources.Project1;
            _project2 = _resources.Project2;

            context.Import(new[] {_project1, _project2});
            callback(new DataResult<CacheContext>(context));
        }

        /// <summary>
        /// Loads the data blow the given key chain.
        /// </summary>
        /// <param name="dataContext">Specifies the data context which contains the underlying data driver root object.</param>
        /// <param name="idChain">The data below the given id should be imported.</param>
        /// <param name="importMonitor">Specifies the monitor which observes the import.</param>
        public void LoadInitialData(GenericDataContextBase dataContext, ValueChain<string> idChain, IDataImportMonitor importMonitor)
        {
            Assert.IsNotNull(idChain);
            Assert.IsNotNull(importMonitor);

            importMonitor.StartSubProgress(string.Empty);

            try
            {
                LoadData(idChain);
            }
            catch (Exception e)
            {
                importMonitor.EndSubProgress(string.Empty, e);
                throw;
            }
            importMonitor.EndSubProgress(string.Empty);
        }

        private void LoadData(ValueChain<string> idChain)
        {
            if (idChain.Top == _project1.UniqueId)
            {
                _resources.ImportProject1Data(_project1);
            }
            else if (idChain.Top == _project2.UniqueId)
            {
                _resources.ImportProject2Data(_project2);
            }
            else
            {
                Assert.Fail("Invalid id chain specified.");
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
